package com.younion.action.consumer.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IMemberCardService;
import com.younion.business.IRegisterUserService;
import com.younion.business.IUserService;
import com.younion.model.LoginResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.RegisterUserVO;
import com.younion.vo.UserVO;

public class LoginConsumer extends AbstractRequestConsumer {
	private IRegisterUserService registerUserService;
	private IUserService userService;
	private IMemberCardService memberCardService;

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return login(request, response);
	}

	public String login(HttpServletRequest request, HttpServletResponse response) {
		String login_id = request.getParameter("login_id");
		String password = request.getParameter("password");
		LoginResult loginResult = new LoginResult();
		boolean succesLogin = false;
		ResultObject resultObject = new ResultObject();
		try {
			Map paramMap = new HashMap();
			paramMap.put("loginId", login_id);
			paramMap.put("password", password);
			List registUserVOs = (List) registerUserService.getByMap(paramMap);
			// app user
			if (registUserVOs != null && registUserVOs.size() > 0) {
				RegisterUserVO registUserVO = (RegisterUserVO) registUserVOs.get(0);
				loginResult.setUser_name(registUserVO.getLoginId());
				loginResult.setUser_token(registUserVO.getUserToken());
				// TODO add user'integral
				loginResult.setUser_integral("0");
				loginResult.setType(ApiConstant.APP_USER_FLAG);
				succesLogin = true;
				//登录成功需要更新该用户下所有的卡为“手机会员卡”
				memberCardService.updateToMobileUser(registUserVO.getId());
			} else { // web admin
				paramMap.remove("loginId");
				paramMap.put("account", login_id);
				List webUserVOs = (List) userService.getByMap(paramMap);
				if (webUserVOs != null && webUserVOs.size() > 0) {
					UserVO userVO = (UserVO) webUserVOs.get(0);
					loginResult.setUser_integral("0");
					loginResult.setUser_name(userVO.getName());
					loginResult.setUser_token(userVO.getUserToken());
					loginResult.setType(ApiConstant.ADMIN_USER_FLAG);
					succesLogin = true;
				}
			}
			if (succesLogin) {
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
			} else {
				resultObject.setStatus(ApiConstant.FALSE_STATUS);
				resultObject.setMsg(ApiConstant.LOGIN_ERROR);
			}
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		resultObject.setResult(loginResult);
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setRegisterUserService(IRegisterUserService registerUserService) {
		this.registerUserService = registerUserService;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

}
